package com.ruoyi.trigger.listener;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.good.service.ISkuStock;
import com.ruoyi.trigger.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @Description
 * @Author wkm
 * @Date 2024/12/27
 **/
@Slf4j
@Component
public class GoodSkuStockZeroCustomer {
    @Value("${spring.rabbitmq.topic.send_message}")
    private String topic;

    @Resource
    private ISkuStock skuStock;

    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.send_message}"))
    public void listener(String message){
        try{
            log.info("监听商品库存消耗为0的消息 topic: {} message: {}",topic,message);
            BaseEvent.EventMessage<Long> eventMessage = JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<Long>>() {
            }.getType());
            Long goodsId = eventMessage.getMessage();
            skuStock.clearGoodSkuStock(goodsId);
            skuStock.clearQueueValue();
        }catch (Exception e){
            log.error("监听用户发送消息失败 topic: {} message: {}",topic,message);
            throw e;
        }
    }

}
